/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import DTO.CFields;
import DTO.Cfile;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
///long
public class CfileBUS extends IData<Cfile>{

    @Override
    public Cfile add(Cfile item) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected void add(Cfile item, int t) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void addrows(List<Cfile> list, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Cfile Find(Cfile item, List<Cfile> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected Object OnSubmit(Cfile item, int t, SubConfig config) {
        Object result = 0;
        String mysql = "";
        PreparedStatement cs;      
        try{
            if(t==0){
            mysql = "INSERT INTO "+CFields.files+"(" +
                CFields.project_id + "," + 
                CFields.detail_user_id + "," + 
                CFields.user_id + "," + 
                CFields.path + "," + 
                CFields.file_name + "," +
                CFields.size + "," +
                CFields.type + "," + 
                CFields.project_state + "," + 
                CFields.isdelete+ ") " +
                " VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?);";
        
                cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);  
                cs.setLong(1, item.getProject_id());
                cs.setLong(2, item.getDetail_user_id());
                cs.setLong(3, item.getUser_id());
                cs.setString(4, item.getPath());
                cs.setString(5, item.getFile_name());
                cs.setDouble(6, item.getSize());
                cs.setString(7, item.getType());
                cs.setBoolean(8, item.isProject_state());
                cs.setBoolean(9, item.isIsdelete());
                
                cs.executeUpdate();
                ResultSet keyResultSet = cs.getGeneratedKeys();
                if (keyResultSet.next()) {
                    result = (long) keyResultSet.getInt(1);      
                }
            }else if (t==1){
                mysql = "update " + CFields.files +" set " +
                CFields.project_id + "= ?," + 
                CFields.detail_user_id + "= ?," + 
                CFields.user_id + "= ?," + 
                CFields.path + "= ?," + 
                CFields.file_name + "= ?," +
                CFields.size + "= ?," +
                CFields.type + "= ?," + 
                CFields.project_state + "= ?," + 
                CFields.isdelete+ "= ? " +
                " Where (" + CFields.id + "= ?);";
                cs = config.connec.prepareStatement(mysql);  
                
                cs.setLong(1, item.getProject_id());
                cs.setLong(2, item.getDetail_user_id());
                cs.setLong(3, item.getUser_id());
                cs.setString(4, item.getPath());
                cs.setString(5, item.getFile_name());
                cs.setDouble(6, item.getSize());
                cs.setString(7, item.getType());
                cs.setBoolean(8, item.isProject_state());
                cs.setBoolean(9, item.isIsdelete());
                cs.setLong(10, item.getId());
                
                cs.executeUpdate();
                ResultSet keyResultSet = cs.getGeneratedKeys();
                if (keyResultSet.next()) {
                    result = (long) keyResultSet.getInt(1); 
                }
            }else if (t==2){
                mysql = "update " + CFields.files +" set " +
                CFields.isdelete+ "= true " +
                "Where (" + CFields.id + "= ?);";
                cs = config.connec.prepareStatement(mysql);  
                
                cs.setLong(1, item.getId());
                
                cs.executeUpdate();
                ResultSet keyResultSet = cs.getGeneratedKeys();
                if (keyResultSet.next()) {
                    result = (long) keyResultSet.getInt(1);  
                }
            }
        } catch (SQLException ex) {
                Logger.getLogger(UserBUS.class.getName()).log(Level.SEVERE, null, ex);
        }       
        return result;
    }
    
    @Override
    public Object insertOnSubmit(Cfile item, SubConfig config) {
        return this.OnSubmit(item, 0, config);
    }

    @Override
    public Object updateOnSubmint(Cfile item, SubConfig config) {
        return this.OnSubmit(item, 1, config);
    }

    @Override
    public Object deleteOnSubmint(Cfile item, SubConfig config) {
        return this.OnSubmit(item, 2, config);
    }

    @Override
    protected Cfile getItem(ResultSet rs) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<Cfile> getAll(SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
